﻿using Microsoft.EntityFrameworkCore;
using Loong.Domain.Uow;
using Loong.Extensions;

namespace Loong.EntityFrameworkCore.Uow
{
    public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbContext>
        where TDbContext : DbContext
    {
        private readonly ICurrentUnitOfWorkProvider _currentUnitOfWorkProvider;

        public UnitOfWorkDbContextProvider(ICurrentUnitOfWorkProvider currentUnitOfWorkProvider)
        {
            _currentUnitOfWorkProvider = currentUnitOfWorkProvider;
        }

        public TDbContext GetDbContext()
        {
            return _currentUnitOfWorkProvider.Current.GetDbContext<TDbContext>();
        }

        public int? GetTimeoutSeconds()
        {
            return _currentUnitOfWorkProvider.Current.Options.Timeout?.TotalSeconds.To<int>();
        }
    }
}
